/**
 * Created by zhongb on 2017/4/11.
 */
var $ = require('jquery');
require('easyui');
var $alert = require('libs/alert');
$(function () {
    //获取url参数
    var refundId = $.getUrlParam("id");
    var status = {
        unsaved: '未保存',
        uncommit: '未提交',
        commit: '已提交',
        recieved: '已收货',
        processed: '已处理',
        processing: '处理中',
        shelveing: '上架中',
        invalid: '已取消'
    };
    fillData(refundId);
    tableInit();
    function fillData(refundId) {
        $.get('api/v1/p/mms/refund/load',{id: refundId},function (res) {
            if(res.code == 200){
                var data = res.data;
                var $firstTarget = $("#refundInfo").find("span");
                //元素赋值
                $firstTarget.eq(0).text(data.refundNo);
                $firstTarget.eq(1).text(data.entName);
                $firstTarget.eq(2).text(data.refundWayNo);
                $firstTarget.eq(3).text(data.warehouseName);
                $firstTarget.eq(4).text(status[data.status]);
                $firstTarget.eq(5).text(data.submitDt);
                var createType = '';
                if(data.createType === 'self_care'){
                    createType = '退货自理';
                }else if(data.createType === 'return_mail'){
                    createType = '回邮单退货';
                }else if(data.createType === 'O_return'){
                    createType = '订单回退';
                }
                $firstTarget.eq(6).text(createType);
                $firstTarget.eq(7).text(data.refundReason);
                //组装datagrid数据源
                var orderGoods = data.orderGoods;
                var tableData = {};
                tableData.total = orderGoods.length;
                tableData.rows = orderGoods;
                $("#mainTable").datagrid('loadData',tableData);
            }else{
                $alert(res.message);
            }
        });
    }
    function tableInit() {
        $("#mainTable").datagrid({
            data: [],
            border : false,
            idField : 'code',
            rownumbers: true,
            nowrap: false,
            columns:[[
                {
                    field : 'code',
                    title : '商品编码',
                    width : '20%'
                },{
                    field : 'chName',
                    title : '商品名称',
                    width : '20%'
                },{
                    field : 'refundNums',
                    title : '商品数量',
                    width : '10%'
                },{
                    field : 'processWay',
                    title : '处理方式',
                    width : '11%',
                    formatter: function(value,row,index){
                        switch (value){
                            case 0: return '再次上架';
                            case 1: return '拍照处理';
                            default: return '';
                        }
                    }
                },{
                    field : 'remark',
                    title : '客户端备注',
                    width : '20%',
                    formatter: function(value,row,index){
                        return "<textarea rows='2' style='width: 100%;resize: none;' disabled>"+value+"</textarea>";
                    }
                },{
                    field : 'backRemark',
                    title : '仓库端备注',
                    width : '20%',
                    formatter: function(value,row,index){
                        return "<textarea rows='2' style='width: 100%;resize: none;' disabled>"+value+"</textarea>";
                    }
                }
                ]],
            onClickRow: function (rowIndex, rowData) {
                $(this).datagrid('unselectRow', rowIndex);
            }
        });
    }
});
